#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QFileDialog>
#include <QPixmap>
#include <QTimer>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_btnSelectModel_clicked()
{
    QString file = QFileDialog::getOpenFileName(this, tr("选择模型文件"), "", tr("Model Files (*.pth *.onnx *.h5 *.rknn);;All Files (*)"));
    if (!file.isEmpty()) {
        modelPath = file;
        ui->labelStatus->setText(tr("检测状态: 已选择模型"));
    }
}

void MainWindow::on_btnSelectImage_clicked()
{
    QString file = QFileDialog::getOpenFileName(this, tr("选择图片"), "", tr("Images (*.png *.jpg *.bmp);;All Files (*)"));
    if (!file.isEmpty()) {
        imagePath = file;
        QPixmap pix(file);
        ui->labelImageLeft->setPixmap(pix.scaled(ui->labelImageLeft->size(), Qt::KeepAspectRatio, Qt::SmoothTransformation));
        ui->labelStatus->setText(tr("检测状态: 已选择图片"));
    }
}

void MainWindow::fillDetectResultTable()
{
    ui->tableDetectResult->setRowCount(1);
    QStringList categories = {"灰尘"};
    QStringList confidences = {"0.66"};
    for (int i = 0; i < 1; ++i) {
        ui->tableDetectResult->setItem(i, 0, new QTableWidgetItem(QString::number(i+1)));
        ui->tableDetectResult->setItem(i, 1, new QTableWidgetItem(categories[i]));
        ui->tableDetectResult->setItem(i, 2, new QTableWidgetItem(confidences[i]));
    }
}

void MainWindow::on_btnStartDetect_clicked()
{
    if (modelPath.isEmpty() || imagePath.isEmpty()) {
        ui->labelStatus->setText(tr("检测状态: 请先选择模型和图片"));
        return;
    }
    
    ui->labelStatus->setText(tr("检测状态: 检测中..."));
    ui->progressBar->setValue(0);
    // 模拟进度条
    QTimer* timer = new QTimer(this);
    connect(timer, SIGNAL(timeout()), this, SLOT(onDetectionTimerTimeout()));
    timer->start(200);
}

void MainWindow::onDetectionTimerTimeout()
{
    int val = ui->progressBar->value();
    if (val < 100) {
        ui->progressBar->setValue(val + 10);
    } else {
        QTimer* timer = qobject_cast<QTimer*>(sender());
        if (timer) {
            timer->stop();
            timer->deleteLater();
        }
        ui->labelStatus->setText(tr("检测状态: 完成"));
        
        // 检测完成后显示结果图片
        QPixmap pix(":/custom_detection_result.jpg");
        ui->labelImageRight->setPixmap(pix.scaled(ui->labelImageRight->size(), Qt::KeepAspectRatio, Qt::SmoothTransformation));
        
        // 检测完成后显示专家建议
        ui->textExpertAdvice->setPlainText(tr("1. 建议及时清理光伏板表面的灰尘，以保证发电效率。\n2. 定期检查光伏板表面，防止灰尘、污渍等覆盖。\n3. 如遇持续灰尘覆盖，建议增加清洁频率。\n4. 检查周边环境，减少扬尘源对光伏板的影响。"));
        
        // 检测完成后填充检测结果表格
        fillDetectResultTable();
    }
}

void MainWindow::on_sliderConfidence_valueChanged(int value)
{
    double conf = value / 100.0;
    ui->labelConfidenceValue->setText(QString::number(conf, 'f', 2));
}

void MainWindow::on_sliderIOU_valueChanged(int value)
{
    double iou = value / 100.0;
    ui->labelIOUValue->setText(QString::number(iou, 'f', 2));
}

void MainWindow::on_btnSaveResult_clicked()
{
    QPixmap pix = ui->labelImageRight->pixmap() ? *ui->labelImageRight->pixmap() : QPixmap();
    if (pix.isNull()) {
        ui->labelStatus->setText(tr("检测结果图片为空，无法保存"));
        return;
    }
    QString fileName = QFileDialog::getSaveFileName(this, tr("保存检测图片"), "detect_result.jpg", tr("Images (*.png *.jpg *.bmp);;All Files (*)"));
    if (!fileName.isEmpty()) {
        if (pix.save(fileName)) {
            ui->labelStatus->setText(tr("检测图片已保存: %1").arg(fileName));
        } else {
            ui->labelStatus->setText(tr("保存失败"));
        }
    }
}
